/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 2 -*- */
/**
* @headerfile dbus.hpp
* 
* Header for easy comunication aplications through Dbus.
*
* @author Miguel Ángel Vílchez López 2008 <sacra@openmatica.es> 
*
* Codigo is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* Codigo is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License along
* with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _MOKARDU_DBUS_HPP_
#define _MOKARDU_DBUS_HPP_

#include <dbus/dbus.h>
#include "debug.hpp"

/**
* @class CMDbus
* @brief Comunication through dbus.
*/

class CMDbus
{

	private:
	
		/**
		* The log class.
		*/
		CMDebug debug;
		
		/**
		* The connection.
		*/
		DBusConnection* conn;

		/**
		* The error buffer.
		*/	
		DBusError err;

		/**
		*
		*/
		DBusMessageIter args;
		
		/**
		*
		*/
		DBusMessage* msg;

		/**
		* The name of the interface.
		*/
		char* name;

		/**
		* The name of the signal.
		*/
		char* signal;
		
		/**
		* The buffer size.
		*/
		unsigned int buffer_size;
		
		/**
		* Initialize the bus connection.
		*
		* @return 0 if correct, -1 if error.
		*/
		int init();
		
	
	public:
	
		/**
		* Constructor.
		*
		* @param[in] name The name of the interface.
		* @param[in] signal The name of the signal.
		* @param[in] buffer The buffer size.
		* @param[in] debug Boolean indicate the debug activation.
		* @param[in] filename The file for logging.
		*/
		CMDbus(const char* name, const char* signal, unsigned int buffer,bool debug,const char* filename);
		
		/**
		* Destructor.
		*/
		~CMDbus();
		
		/**
		* Make a rule to catch a signal.
		*
		* @return 0 if correct, -1 if error.
		*/
		int ruleSignal();
		
		/**
		* Receive a signal.
		*
		* @param[out] signal The variable to fill with data received.
		* @return The number of bytes received, -1 if error.
		*/
		int recvSignal(char* signal);
		
		/**
		* Send a signal.
		*
		* @param[in] signal The signal to send.
		* @return 0 if correct, -1 if error.
		*/
		int sendSignal(char* signal);

};

#endif // _MOKARDU_DBUS_HPP_
